package nc.bs.fa.listener;

import java.util.List;

import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.itf.fip.relation.IFipRelation;
import nc.itf.uap.IUAPQueryBS;
import nc.vo.am.common.MappedBaseVO;
import nc.vo.fa.voucher.ClosebookVoucherVO;
import nc.vo.fa.closebook.CloseBookVO;
import nc.vo.fip.relation.FipRelationVO;
import nc.vo.glcom.tools.GLPubProxy;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDateTime;

public class DepBeforeListener implements IBusinessListener{

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		IUAPQueryBS QueryBS = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		MappedBaseVO vo = (MappedBaseVO) ((BusinessEvent)event).getObject();
		StringBuffer sb = new StringBuffer(" nvl(dr,0)=0 and ");
		sb.append(" pk_org = '" + vo.getAttributeValue("pk_org")+ "' and ");
		sb.append(" pk_accbook = '" +vo.getAttributeValue("pk_accbook")+"' and ");
		sb.append(" accyear= '"+vo.getAttributeValue("accyear")+"' and ");
		sb.append(" period= '"+vo.getAttributeValue("period")+"'");
		List<ClosebookVoucherVO> result = (List<ClosebookVoucherVO>) QueryBS.retrieveByClause(ClosebookVoucherVO.class, sb.toString());
		if(result!=null && result.size()>0){
			ClosebookVoucherVO logVO = result.get(0);
//			List<FipRelationVO> deleteVO = (List<FipRelationVO>) QueryBS.retrieveByClause(FipRelationVO.class, "nvl(dr,0)=0 and DES_RELATIONID='"+logVO.getPk_voucher()+"'");
//			NCLocator.getInstance().lookup(IFipRelation.class).delete(deleteVO.toArray(new FipRelationVO[0]));
			GLPubProxy.getRemoteVoucher().deleteByPk(logVO.getPk_voucher());
			logVO.setDr(1);
			logVO.setDeletetime(new UFDateTime());
			logVO.setStatus(VOStatus.UPDATED);
			new BaseDAO(InvocationInfoProxy.getInstance().getUserDataSource()).updateVO(logVO); 
		}
	}
}
